package com.lxw.nonghe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxw.nonghe.entity.CertificateChangeLog;
import com.lxw.nonghe.mapper.CertificateChangeLogMapper;
import com.lxw.nonghe.service.CertificateChangeLogService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 慢病证变更记录表 服务实现类
 * </p>
 *
 * @author lxw
 * @since 2025-05-14
 */
@Service
public class CertificateChangeLogServiceImpl
        extends ServiceImpl<CertificateChangeLogMapper, CertificateChangeLog>
        implements CertificateChangeLogService {

    @Override
    public Page<CertificateChangeLog> getChangeLogPage(
            int current, int size,
            Integer certificateId, String changeType, String operatorName) {

        QueryWrapper<CertificateChangeLog> wrapper = new QueryWrapper<>();

        // 慢病证ID精确查询
        if (certificateId != null) {
            wrapper.eq("certificate_id", certificateId);
        }

        // 变更类型精确查询
        if (StringUtils.isNotBlank(changeType)) {
            wrapper.eq("change_type", changeType.trim());
        }

        // 操作员姓名模糊查询
        if (StringUtils.isNotBlank(operatorName)) {
            wrapper.like("operator_name", operatorName.trim());
        }

        // 排序，最新变更的在前面
        wrapper.orderByDesc("change_time");

        // 执行分页查询
        return this.page(new Page<>(current, size), wrapper);
    }
    // 新增条件查询实现
    @Override
    public Page<CertificateChangeLog> queryChangeLogs(
            CertificateChangeLog queryParam,
            int current, int size) {

        QueryWrapper<CertificateChangeLog> wrapper = new QueryWrapper<>();

        // 慢病证ID精确查询
        if (queryParam.getCertificateId() != null) {
            wrapper.eq("certificate_id", queryParam.getCertificateId());
        }

        // 变更类型精确查询
        if (StringUtils.isNotBlank(queryParam.getChangeType())) {
            wrapper.eq("change_type", queryParam.getChangeType().trim());
        }

        // 变更原因模糊查询
        if (StringUtils.isNotBlank(queryParam.getChangeReason())) {
            wrapper.like("change_reason", queryParam.getChangeReason().trim());
        }

        // 操作员ID精确查询
        if (queryParam.getOperatorId() != null) {
            wrapper.eq("operator_id", queryParam.getOperatorId());
        }

        // 操作员姓名模糊查询
        if (StringUtils.isNotBlank(queryParam.getOperatorName())) {
            wrapper.like("operator_name", queryParam.getOperatorName().trim());
        }

        // 变更时间范围查询 - 大于等于
        if (queryParam.getChangeTime() != null) {
            wrapper.ge("change_time", queryParam.getChangeTime());
        }

        // 备注模糊查询
        if (StringUtils.isNotBlank(queryParam.getRemark())) {
            wrapper.like("remark", queryParam.getRemark().trim());
        }

        // 排序，最新变更的在前面
        wrapper.orderByDesc("change_time");

        // 执行分页查询
        return this.page(new Page<>(current, size), wrapper);
    }
}
